using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using UlyssesAgenda.Repositories;
using UlyssesAgenda.Repositories.Blog;

namespace UlyssesAgenda.Web.Controllers
{
    [HandleError]
    public class BlogController : Controller
    {
        private BlogEntryRepository repository;

        public BlogController() : this(null) { }

        public BlogController(BlogEntryRepository blogEntryRepository)
        {
            if (blogEntryRepository != null)
                repository = blogEntryRepository;
            else
                repository = RepositoryFactory.GetRepository<BlogEntryRepository>();
        }

        //
        // GET: /Blog/

        public ActionResult Index(int? page)
        {
            const int pageSize = 25;

            BlogEntryInfo[] blogPosts = repository.GetBlogEntries(pageSize, page.GetValueOrDefault(0));

            return View(blogPosts);
        }

        //
        // GET: /Blog/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Blog/Create
        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(BlogEntryInfo post)
        {
            if (ModelState.IsValid)
            {
                repository.CreateBlogEntry(post.Title, User.Identity.Name, post.PostHtml);
            }

            return RedirectToAction("Index");
        }
    }
}
